package com.notecalendar.security;


import org.springframework.dao.DataAccessException;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.evernote.edam.type.User;
import com.notecalendar.EvernoteBean;
import com.notecalendar.exception.EvernoteBeanException;
import com.notecalendar.model.Evernote_Login;
import com.notecalendar.ws.beans.EvernoteUser;

/**
 * Class for authenticate users
 * @author bcamcereijo
 *
 */
public class NoteCalendarSecurity implements AuthenticationProvider, UserDetailsService {
	   
		
	//injected via spring
	private EvernoteBean evernoteBean;
	
	public void setEvernoteBean(EvernoteBean evernoteBean) {
		this.evernoteBean = evernoteBean;
	}
	
	private String proxySet = "false";
	public void setProxySet(String proxySet) {
		this.proxySet = proxySet;
		System.out.println("proxySet:"+proxySet);
		if("true".equalsIgnoreCase(proxySet)){
			System.setProperty("proxySet",proxySet);
		    System.setProperty("https.proxyPort","8080");
		    System.setProperty("https.proxyHost","proxy.map.es");
		}
	}
	
	/**
	 * 
	 */
	
	public Authentication authenticate(Authentication authentication)
			throws AuthenticationException {
		if (authentication instanceof UsernamePasswordAuthenticationToken){
			UsernamePasswordAuthenticationToken token = 
				(UsernamePasswordAuthenticationToken) authentication;
			String username = (String)token.getPrincipal();
			String password = (String)token.getCredentials();
			
			System.out.println("Llega username:"+username+" pass:"+password);
			
			try{
				//valdiate user/pass in evernote system
				Evernote_Login evernote_login = new Evernote_Login();
					evernote_login.setEvernote_user(username);
					evernote_login.setEvernote_pass(password);
				
				//String authToken = evernoteBean.evernoteSystemInit(evernote_login);
				EvernoteUser user = evernoteBean.evernoteSystemInit(evernote_login);
				System.out.println("authToken: "+user.getAuthToken());
				System.out.println("Credentials first : "+authentication.getCredentials());
				
				UsernamePasswordAuthenticationToken userPass = new 
					UsernamePasswordAuthenticationToken(username, password);
				//authtoken from evernote into security token details
				//userPass.setDetails(user.getAuthToken());
				userPass.setDetails(user);
			    
				return userPass;
				
			}catch(EvernoteBeanException e){ 
				throw new BadCredentialsException("Username and/or password incorrect. Try again.");
			}
			
		}
		
		throw new BadCredentialsException("Incorrect credentials ");
	}

	public boolean supports(Class arg0) {
		System.out.println("supports class :"+arg0);
		return true;
		//return false;
	}


	public UserDetails loadUserByUsername(String arg0)
			throws UsernameNotFoundException, DataAccessException {
		System.out.println("loadUserByUsername:"+arg0);
		return null;
	}
	
	


}
